package www.dianbaozu.com.ui.server

import android.os.Bundle
import android.view.View
import androidx.fragment.app.viewModels
import androidx.lifecycle.lifecycleScope
import dagger.hilt.android.AndroidEntryPoint
import www.dianbaozu.com.R
import www.dianbaozu.com.databinding.FragmentChannelCreateBinding
import www.dianbaozu.com.extension.requestUniversally
import www.wen.com.common.lib.base.fragment.BaseLoadingFragment
import www.wen.com.common.lib.base.module.BaseApiViewModel
import www.wen.com.common.lib.base.module.BaseViewModel
import www.wen.com.common.lib.data.entities.*
import www.wen.com.common.lib.extension.*
import www.wen.com.common.lib.helper.loading.KeyHelper
import www.wen.com.common.lib.helper.loading.KeyHelper.BUNDLE_DATA
import www.wen.com.common.lib.helper.loading.KeyHelper.CHANNEL_CREATE_FRAGMENT_UI_KEY
import www.wen.com.common.lib.helper.loading.TitleBarUniversalAdapter
import www.wen.com.common.lib.util.InputFilterUtil


/**
 * @author WEN
 * @Description:创建频道
 * @date 2020//25 17:48
 */
@AndroidEntryPoint
class ChannelCreateFragment : BaseLoadingFragment<FragmentChannelCreateBinding>(),
        View.OnClickListener {

    private lateinit var mBundleEntitySealed: BundleEntitySealed

    private lateinit var mBundleEntityFromServerSealed: BundleEntitySealed.BundleEntityFromServerSealed

    private lateinit var mBundleEntityFromRecommendSealed: BundleEntitySealed.BundleEntityFromRecommendSealed

    private var mGroupId: Int = -1

    private var mServerId: String = ""

    private val mBaseApiViewModel: BaseApiViewModel by viewModels()

    override val viewModel: BaseViewModel
        get() = mBaseApiViewModel

    override fun isEnableLoadingHelper() = true

    override fun actionRight() {
        if (dataBinding.etPleaseEnterAGroupName.trimProxy().isBlank()) {
            toast("请输入频道名称")
            return
        }

        if (dataBinding.etPleaseEnterAGroupName.trimProxy().checkHasLineFeed()) {
            toast("频道名称不能换行")
            return
        }
        if (dataBinding.etIntroduction.trimProxy().checkHasLineFeed()) {
            toast("频道介绍不能换行")
            return
        }
        mBaseApiViewModel.requestUniversally(
                viewModelScope = lifecycleScope,
                request = { appDataManage ->
                    appDataManage.requestCreateChannel(
                            RequestBodyParameter(
                                    channelName = dataBinding.etPleaseEnterAGroupName.trimProxy(),
                                    channelDesc = dataBinding.etIntroduction.trimProxy(),
                                    groupId = mGroupId
                            )
                    )
                }
        ) { channelEntity ->
            doingSomething(channelEntity)
        }
    }

    private fun doingSomething(channelEntity: ChannelEntity) {
        when (mBundleEntitySealed) {
            is BundleEntitySealed.BundleEntityFromServerSealed -> {
//                postObject(KeyHelper.REFRESHING_SERVER_LIST_KEY1, Object())
//                actionLeft()
                //选中对应的服务器并展示
                postObject(KeyHelper.REFRESHING_SERVER_LIST_KEY3,
                        ServerUpdateInfoEntity(
                                teamId = channelEntity.teamId,
                                serverId = channelEntity.serverId,
                                serverName = mBundleEntityFromServerSealed.serverName))
                //主页切换服务器页面
                postObject(KeyHelper.MAIN_VIEWPAGER_POSITION_KEY, ViewPagePosition(0))
                //返回主页
                popProxy(R.id.mainFragment)
            }
            is BundleEntitySealed.BundleEntityFromRecommendSealed -> {
//                actionLeft()
                //选中对应的服务器并展示
                postObject(KeyHelper.REFRESHING_SERVER_LIST_KEY3,
                        ServerUpdateInfoEntity(
                                teamId = channelEntity.teamId,
                                serverId = channelEntity.serverId,
                                serverName = mBundleEntityFromRecommendSealed.serverName))
                //主页切换服务器页面
                postObject(KeyHelper.MAIN_VIEWPAGER_POSITION_KEY, ViewPagePosition(0))
                //返回主页
                popProxy(R.id.mainFragment)
            }
        }
    }

    override fun getTitleBarUniversalAdapter(): TitleBarUniversalAdapter = TitleBarUniversalAdapter(
            requireActivity(),
            tilte = getString(R.string.create_channel),
            isShowRightText = true,
            rightText = getString(R.string.create_channel_enter),
            rightTextColor = getColorAgency(R.color.colorFFFFFF),
            action = ::titleAction
    )

    override fun getLayoutId(): Int = R.layout.fragment_channel_create

    override fun lazyInitView() {
        mBundleEntitySealed = requireArguments().getParcelable(BUNDLE_DATA)!!

        dataBinding.etPleaseEnterAGroupName.filters = InputFilterUtil().excludedExpression().excludedN().maxLength(16).build()
        dataBinding.etIntroduction.filters = InputFilterUtil().excludedExpression().excludedN().maxLength(20).build()

        when (mBundleEntitySealed) {

            is BundleEntitySealed.BundleEntityFromServerSealed -> {
                mBundleEntityFromServerSealed = mBundleEntitySealed as BundleEntitySealed.BundleEntityFromServerSealed
                initUI(mBundleEntityFromServerSealed.groupId, mBundleEntityFromServerSealed.groupName, mBundleEntityFromServerSealed.serverId)
            }
            is BundleEntitySealed.BundleEntityFromRecommendSealed -> {
                mBundleEntityFromRecommendSealed = mBundleEntitySealed as BundleEntitySealed.BundleEntityFromRecommendSealed
                initUI(mBundleEntityFromRecommendSealed.groupId, mBundleEntityFromRecommendSealed.groupName, mBundleEntityFromRecommendSealed.serverId)
            }

        }
        observeListen()
    }

    private fun initUI(groupId: Int, groupName: String, serverId: String) {
        mServerId = serverId
        mGroupId = groupId
        dataBinding.run {
            tvGroupName.text = groupName
        }
    }

    private fun observeListen() {
        observeObject(CHANNEL_CREATE_FRAGMENT_UI_KEY, this, GroupingEntity::class.java) {
            dataBinding.tvGroupName.text = groupName
            mGroupId = groupId
        }
    }

    override fun onClick(v: View) {


        mBaseApiViewModel.requestUniversally(
                viewModelScope = lifecycleScope,
                isShowToastTip = false,
                request = { appDataManage ->
                    appDataManage.requestServerGroup(mServerId)
                }
        ) { groups ->
            nav(R.id.action_channelCreateFragment_to_groupingSortingFragment, Bundle().apply {
                groups.first { group ->
                    mGroupId == group.groupId
                }.also {
                    it.isSelected = true
                    putParcelableArrayList(BUNDLE_DATA, groups as ArrayList<GroupingEntity>)
                }
            })
        }
    }

}



